/**
 * @ProjectName:exam
 * @FileName: DriverEntiryController.java
 * @PackageName: edu.xpu.project.user.controller
 * @Company:北京有生博大软件有限公司（西安分公司）
 * @Copyright (c) 2017,RiseSoft  All Rights Reserved.
 * @date 2017年4月29日 下午6:27:00
 */
package edu.xpu.project.user.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.xpu.project.user.entity.DriverEntity;
import edu.xpu.project.user.entity.ProjectUser;
import edu.xpu.project.user.service.DriverEntityService;

/**
 * @ClassName: DriverEntiryController.java
 * @Description: 驾驶员的控制器
 *
 * @author liukang
 * @date 2017年4月29日 下午6:27:00
 * @version 
 * @since JDK 1.6
 */
@Controller
@RequestMapping("driver")
public class DriverEntiryController {
	
	@Autowired
	private DriverEntityService driverEntityService;
	
	/**
	  * @MethodName: driverDetails
	  * @Description: 驾驶员详情
	  * @Author liukang
	  * @date 2017年5月23日  下午12:15:05
	 */
	@RequestMapping(value="driverDetails/{id}")
	public String driverDetails(@PathVariable(value="id") Long id, ModelMap modelMap) {
		DriverEntity driverEntity = driverEntityService.get(id);
		modelMap.addAttribute("driverEntity", driverEntity);
		return "driver/driverDetail";
	}
	
	/**
	 * @describe 司机删除
	 * @date 2017年5月22日
	 * @author liukang
	 */
	@ResponseBody
	@RequestMapping(value="driverDel/{id}")
	public String driverDel(@PathVariable(value="id") Long id) {
		try {
			driverEntityService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"status\":\"error\"}";
		}
		return "{\"status\":\"success\"}";
	}
	
	/**
	 * @describe 驾驶员添加
	 * @date 2017年5月9日
	 * @author liukang
	 */
	@RequestMapping(value="/driverAdd")
	public String driverAdd(DriverEntity driverEntity, ModelMap modelMap, @ModelAttribute("sessionUser") ProjectUser sessionUser) {
		DriverEntity saveObj = null;
		//如果为新增
		if (driverEntity.getId() == null) {
			driverEntity.setCreateDate(new Date());
			//获取sessionUser
			driverEntity.setCreatePersonId(sessionUser.getId());
			driverEntity.setCreatePersonName(sessionUser.getName());
			saveObj = driverEntity;
		} else {
			DriverEntity driverEntity2 = driverEntityService.get(driverEntity.getId());
			driverEntity2.setName(driverEntity.getName());
			driverEntity2.setLoginName(driverEntity.getLoginName());
			driverEntity2.setSex(driverEntity.getSex());
			driverEntity2.setBirthDay(driverEntity.getBirthDay());
			driverEntity2.setTelPhone(driverEntity.getTelPhone());
			driverEntity2.setDrivingExp(driverEntity.getDrivingExp());
			saveObj = driverEntity2;
		}
		driverEntityService.saveOrUpdate(saveObj);
		return "redirect:driverList";
	}
	
	/**
	  * @MethodName: toDriverAdd
	  * @Description: 定向到驾驶员信息的添加页面
	  * @return String    返回类型
	  * @throws
	  * 
	  * @Author liukang
	  * @date 2017年4月29日  下午6:30:58
	 */
	@RequestMapping(value="toDriverAdd")
	public String toDriverAdd() {
		
		return "driver/driverAdd";
	}
	
	
	/**
	  * @MethodName: driverInfoList
	  * @Description: 驾驶员信息的列表展示
	  * @param pageNumber
	  * @param pageSize
	  * @param driverEntity
	  * @param modelMap
	  * @return String    返回类型
	  * @throws
	  * 
	  * @Author liukang
	  * @date 2017年4月29日  下午6:30:03
	 */
	@RequestMapping(value="driverList")
	public String driverInfoList(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			DriverEntity driverEntity, ModelMap modelMap) {
		Page<DriverEntity> pageByCriterias = driverEntityService.getPageByCriterias(pageNumber, pageSize, driverEntity);
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("driverEntity", driverEntity)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize);
		return "driver/driverList";
	}
	

}

